package com.fire.web.modules.all.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.fire.common.mybatis.mvc.wrapper.EntityWrapper;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.AjaxResult;
import com.fire.web.modules.all.entity.FireDangrousResouceModel;
import com.fire.web.modules.all.request.AddFireDangrousResouceReq;
import com.fire.web.modules.all.service.IFireDangrousResouceService;
import com.fire.web.modules.all.service.IReceiveAreaService;
import com.fire.web.modules.all.service.ISequenceService;
import com.fire.web.modules.all.service.ITMitErrorCodeService;
import com.fire.web.modules.all.utils.WrapperUtil;
import com.fire.web.utils.PageRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/danger")
public class DangerController extends CrudController<IFireDangrousResouceService, FireDangrousResouceModel> {
	private static final Logger LOGGER = LoggerFactory.getLogger(DangerController.class);

	@Autowired
	private ITMitErrorCodeService errorCodeService;
	@Autowired
	private ISequenceService sequenceService;
	@Autowired
	private IFireDangrousResouceService iFireDangrousResouceService;
	@Autowired
	IReceiveAreaService receiveAreaService;

	/**
	 * 查询危险源
	 *
	 * @return
	 */
	@GetMapping("/getDangerList")
	@ResponseBody
	public AjaxResult getDangerList(@RequestParam Map<String, Object> params) {
		LOGGER.info("==================查询危险源开始==================");
		AjaxResult ajaxResult;
		try {
			//遍历参数获得查询条件
			EntityWrapper ew = WrapperUtil.getEw(params);
			//查询
			Page pageBean = iFireDangrousResouceService.selectPage(PageRequest.getPage(), ew);
			//遍历拼接完整地址
			for (Object model : pageBean.getRecords()) {
				FireDangrousResouceModel firemodel = (FireDangrousResouceModel) model;
				String address = getAddress(firemodel.getProvince(), firemodel.getCity(), firemodel.getZone());
				firemodel.setDetailAddress(address + firemodel.getDistrict() + firemodel.getAddress());
			}

			ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "获取成功", pageBean, "获取成功");
		} catch (Exception e) {
			LOGGER.error("DangerController.getDangerList().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================查询危险源结束==================");
		return ajaxResult;
	}

	/**
	 * 增加危险源
	 *
	 * @return
	 */
	@PostMapping("/addDanger")
	@ResponseBody
	public AjaxResult addDanger(AddFireDangrousResouceReq req) {
		LOGGER.info("==================新增危险源开始==================");
		LOGGER.info(req.toString());
		AjaxResult ajaxResult;
		try {
			ajaxResult = iFireDangrousResouceService.addDangrousResouce(req);
		} catch (Exception e) {
			LOGGER.error("DangerController.addDanger().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================新增危险源结束==================");
		return ajaxResult;
	}


	/**
	 * 更新危险源
	 *
	 * @return
	 */
	@PutMapping("/updateDanger")
	@ResponseBody
	public AjaxResult updateDanger(AddFireDangrousResouceReq req) {
		LOGGER.info("==================更新危险源开始==================");
		LOGGER.info(req.toString());
		AjaxResult ajaxResult;
		try {
			ajaxResult = iFireDangrousResouceService.updateDangrousResouce(req);
		} catch (Exception e) {
			LOGGER.error("dangerController.updateDanger().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================更新危险源结束==================");
		return ajaxResult;
	}

	/**
	 * 查询所有省（选择省份）
	 *
	 * @return
	 */
	@GetMapping("/getProvinceOpt")
	@ResponseBody
	public AjaxResult getProvinceOpt() {
		LOGGER.info("==================查询所有省份下拉选项开始==================");
		AjaxResult ajaxResult;
		try {
			ajaxResult = receiveAreaService.getProvinceOpt();
		} catch (Exception e) {
			LOGGER.error("dangerController.provinceOpt().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================查询所有省份下拉选项结束==================");
		return ajaxResult;
	}

	/**
	 * 查询所有市或者区（选择市区）
	 *
	 * @return
	 */
	@GetMapping("/getCityOrZoneOpt/{parentid}")
	@ResponseBody
	public AjaxResult getCityOrZoneOpt(@PathVariable("parentid") String parentid) {
		LOGGER.info("==================查询所有市区下拉选项开始==================");
		AjaxResult ajaxResult;
		try {
			ajaxResult = receiveAreaService.getCityOrZoneOpt(parentid);
		} catch (Exception e) {
			LOGGER.error("dangerController.getCityOrZoneOpt().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================查询所有市区下拉选项结束==================");
		return ajaxResult;
	}

	public String getAddress(String provinceCode, String cityCode, String zonCode) {
		LOGGER.info("省id:" + provinceCode);
		LOGGER.info("市id:" + cityCode);
		LOGGER.info("区id:" + zonCode);
		return receiveAreaService.getAddress(provinceCode, cityCode, zonCode);
	}

}
